<template>
  <!-- 登录 -->
  <view class="content box">
    <view class="box_images">
      <image src="../../static/images/bg_dengluzhuce.png" mode=""></image>
    </view>
    <view class="box_title">
      <view class="boxt">华胥星球</view>
      <view class="boxc">探索星河山海的奥秘</view>
    </view>

    <view class="box_inp">
      <!-- 邮箱输入 -->
      <!-- <u-input
        class="input"
        placeholder="请输入邮箱"
        maxlength="20"
        border="none"
        shape="circle"
        color="#888888"
        :fontSize="inputFont"
        v-model="value"
        @change="change"
        placeholderStyle="color:#888888;padding-left:50rpx"
        v-if="!loginType"
      ></u-input> -->
      <!-- 账号输入 -->
      <u-input
        class="input"
        :placeholder="accountNumber"
        maxlength="20"
        border="none"
        shape="circle"
        color="#888888"
        :fontSize="inputFont"
        v-model="value"
        @change="change"
        placeholderStyle="color:#888888;padding-left:50rpx"
      ></u-input>
      <!-- 密码输入 -->
      <u-input
        class="inputPassword"
        v-if="isPassword"
        type="password"
        placeholder="请输入登录密码"
        maxlength="20"
        border="none"
        shape="circle"
        color="#888888"
        :fontSize="inputFont"
        v-model="password"
        @change="change"
        placeholderStyle="color:#888888;padding-left:50rpx"
      ></u-input>
      <!-- 验证码输入 -->
      <u-input
        class="inputPassword"
        v-else
        type="number"
        placeholder="请输入验证码"
        maxlength="6"
        border="none"
        shape="circle"
        color="#888888"
        :fontSize="inputFont"
        v-model="smsCode"
        @change="change"
        placeholderStyle="color:#888888;padding-left:50rpx"
      >
        <template slot="suffix">
          <u-code
            ref="uCode"
            @change="codeChange"
            seconds="120"
            changeText="Xs"
          ></u-code>
          <view @tap="getCode" class="codeTip">{{ tips }}</view>
        </template></u-input
      >
      <!-- 账号登录 -->
      <view class="inputBottom" v-if="isPassword">
        <!-- 忘记密码 -->
        <view class="forgotPassw" @click="forgotPassword">忘记密码？</view>
        <!-- 验证码登录 -->
        <view class="verCodeLogin" @click="changeLoginMethod">验证码登录</view>
      </view>
      <!-- 验证码登录 -->
      <view class="inputBottom" v-else>
        <!-- 忘记密码 -->
        <view class="forgotPassw"></view>
        <!-- 验证码登录 -->
        <view class="verCodeLogin" @click="changeLoginMethod">密码登录</view>
      </view>

      <view class="login">
        <view class="loginFont" @click="login">登录</view>
      </view>

      <view class="account">
        <span class="noAccount">我还没有账号，</span>
        <span class="registerAccount" @click="regist">注册账号</span>
      </view>
    </view>

    <!-- 其他登录 -->
    <div class="other_login">
      <u-divider text="其他登录"></u-divider>
      <div class="other_login_btn">
        <div class="login_btn_item" v-if="loginType" @click="emailLogin">
          <u--image
            src="../../static/images/icon_dl_youxiang.png"
            width="44px"
            height="44px"
          ></u--image>
          <div class="login_btn_item_text">邮箱</div>
        </div>
        <div class="login_btn_item" v-else @click="emailLogin">
          <u--image
            src="../../static/images/icon_dl_zhanghao.png"
            width="44px"
            height="44px"
          ></u--image>
          <div class="login_btn_item_text">账号</div>
        </div>
      </div>
    </div>

    <!-- 用户服务协议 隐私政策》 -->
    <view class="box_bottom">
      <view @click="selects">
        <view v-if="selected" class="select">
          <image src="../../static/images/xuanzhong.png" mode=""></image>
        </view>
        <view v-else class="select">
          <image src="../../static/images/weixuanzhong.png" mode=""></image>
        </view>
      </view>
      <view>我已阅读并同意</view>
      <view class="user">
        <u-link
          href="https://hx.yitian369.net/protocol.html"
          text="《用户服务协议》"
          color="#55b877"
          class="user_text"
        ></u-link>
      </view>
      <view>和</view>
      <view class="user">
        <u-link
          href="https://hx.yitian369.net/Privacy.html"
          text="《隐私政策》"
          color="#55b877"
          class="user_text"
        ></u-link>
      </view>
    </view>
  </view>
</template>

<script>
import storage from "@/common/storage"; // 缓存封装
export default {
  data() {
    return {
      value: "",
      password: "",
      smsCode: "",
      inputFont: "30rpx",
      isPassword: true,
      selected: false,
      tips: "",
      checked: [],
      accountNumber: "请输入手机号码",
      loginType: true, //true:手机号登录   false:邮箱登录
    };
  },
  onLoad() {
    let token = storage.get("token");
    // console.log(token, "有没有token");
    if (token) {
      uni.navigateTo({
        url: "/pages/index/index",
      });
    }
  },
  methods: {
    // 转换邮箱登录
    emailLogin() {
      this.loginType = !this.loginType;
      this.value = ""
      this.password= ""
      this.smsCode= ""
      if (this.loginType) {
        this.accountNumber = "请输入手机号码";
      } else {
        this.accountNumber = "请输入邮箱";
      }
    },

    changeLoginMethod() {
      this.isPassword = !this.isPassword;
      // if (this.isPassword) {
      //   this.accountNumber = "请输入手机号码";
      // } else {
      //   this.accountNumber = "请输入手机号码";
      // }
    },
    change() {
      // console.log(this.value);
    },
    codeChange(text) {
      this.tips = text;
    },
    getCode() {
      if (this.value == "") {
        if (this.loginType) {
          uni.$u.toast("请先输入手机号");
          return;
        } else {
          uni.$u.toast("请先输入邮箱");
          return;
        }
      }
      if (this.$refs.uCode.canGetCode) {
        if (this.loginType) {
          this.$http
            .sendSmsCaptcha({ mobile: this.value })
            .then((res) => {
              // console.log(res.data, 111111111);

              // 模拟向后端请求验证码
              uni.showLoading({
                title: "正在获取验证码",
              });
              setTimeout(() => {
                uni.hideLoading();
                // 这里此提示会被this.start()方法中的提示覆盖
                uni.$u.toast("验证码已发送");
                // 通知验证码组件内部开始倒计时
                this.$refs.uCode.start();
              }, 2000);
            })
            .catch((error) => {
              // console.log(error, 222222222);
              uni.$u.toast(error);
            });
        } else {
          this.$http
            .send_email({ email: this.value })
            .then((res) => {
              // console.log(res.data, 111111111);

              // 模拟向后端请求验证码
              uni.showLoading({
                title: "正在获取验证码",
              });
              setTimeout(() => {
                uni.hideLoading();
                // 这里此提示会被this.start()方法中的提示覆盖
                uni.$u.toast("验证码已发送");
                // 通知验证码组件内部开始倒计时
                this.$refs.uCode.start();
              }, 2000);
            })
            .catch((error) => {
              // console.log(error, 222222222);
              uni.$u.toast(error);
            });
        }
      } else {
        uni.$u.toast("倒计时结束后再发送");
      }
    },
    regist() {
      uni.navigateTo({
        url: "register",
      });
    },
    forgotPassword() {
      uni.navigateTo({
        url: "forgotPassword",
      });
    },
    selects() {
      this.selected = !this.selected;
    },
    login() {
      if (!this.selected) {
        return uni.$u.toast("请勾选阅读用户服务协议和隐私政策");
      }

      // 账号登录
      if (!this.value) {
        if (this.loginType) {
          return uni.$u.toast("请输入手机号");
        } else {
          return uni.$u.toast("请输入邮箱");
        }
      }
      if (this.isPassword) {
        if (!this.password) {
          return uni.$u.toast("请输入密码");
        }
        this.$http
          .passportLogin({ mobile: this.value, password: this.password, email_or_phone:this.loginType?1:2 })
          .then((res) => {
            // console.log(res.data, 111111111);
            storage.set("token", res.data.token);
            storage.set("user_id", res.data.userId);
            uni.$u.toast("登陆成功");
            setTimeout(() => {
              uni.redirectTo({
                url: "/pages/index/index",
              });
            }, 1500);
          })
          .catch((error) => {
            uni.$u.toast(error);
          });
      } else {
        if (!this.smsCode) {
          return uni.$u.toast("请输入验证码");
        }
        this.$http
          .passportMobile({ mobile: this.value, smsCode: this.smsCode, email_or_phone:this.loginType?1:2 })
          .then((res) => {
            // console.log(res.data, 111111111);
            storage.set("token", res.data.token);
            storage.set("user_id", res.data.userId);
            uni.$u.toast("登陆成功");
            setTimeout(() => {
              uni.redirectTo({
                url: "/pages/index/index",
              });
            }, 1500);
          })
          .catch((error) => {
            uni.$u.toast(error);
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  background: #111111;
  padding: 212rpx 60rpx 0;
  box-sizing: border-box;
  height: 100%;
  position: fixed;
  overflow: auto;
}
.box_images {
  width: 370rpx;
  height: 266rpx;
  position: fixed;
  right: 0;
  top: 0;
  image {
    width: 100%;
    height: 100%;
  }
}
.box_title {
  .boxt {
    font-size: 56rpx;
    font-family: PingFang SC-Bold, PingFang SC;
    font-weight: bold;
    color: #fff;
  }
  .boxc {
    font-size: 30rpx;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight: bold;
    color: #a2a2a2;
    margin-top: 12rpx;
  }
}
.box_inp {
  width: 100%;

  .input {
    background-color: #272c29;
    width: 630rpx;
    height: 96rpx;
    margin-top: 112rpx;
  }
  .inputPassword {
    width: 630rpx;
    height: 96rpx;
    background-color: #272c29;
    margin-top: 40rpx;
    position: relative;

    .codeTip {
      position: absolute;
      right: 50rpx;
      bottom: 26rpx;
      font-size: 30rpx;
      font-weight: 400;
      color: #55b877;
    }
  }

  /deep/.uni-input-input {
    padding-left: 50rpx;
  }

  .inputBottom {
    margin-top: 32rpx;
    display: flex;
    justify-content: space-between;
    padding: 0 30rpx;

    .forgotPassw {
      font-size: 28rpx;
      font-weight: 400;
      color: #adadad;
    }
    .verCodeLogin {
      font-size: 28rpx;
      font-weight: 400;
      color: #55b877;
    }
  }

  .login {
    width: 630rpx;
    height: 96rpx;
    margin-top: 54rpx;
    background: linear-gradient(360deg, #50b674 0%, #aeddbd 100%);
    border-radius: 48rpx 48rpx 48rpx 48rpx;

    .loginFont {
      line-height: 96rpx;
      text-align: center;
      font-size: 30rpx;
      font-weight: 400;
      color: #ffffff;
    }
  }

  .account {
    text-align: center;
    margin-top: 32rpx;
    font-size: 28rpx;

    .noAccount {
      color: #d8d8d8;
    }
    .registerAccount {
      color: #55b877;
    }
  }
}
.other_login {
  margin-top: 260rpx;
  margin-bottom: 100rpx;
  .other_login_btn {
    margin-top: 40rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    .login_btn_item {
      display: flex;
      flex-direction: column;
      align-items: center;
      .login_btn_item_text {
        color: #ffffff;
        margin-top: 10rpx;
        font-size: 24rpx;
      }
    }
  }
}
.box_bottom {
  // position: fixed;
  // bottom: 40rpx;
  margin-bottom: 40rpx;
  display: flex;
  align-items: center;
  font-size: 26rpx;
  color: #888888;
  box-sizing: border-box;
  width: 100%;
  .user {
    color: #55b877;
  }
  /deep/.user_text {
    font-size: 26rpx !important;
  }
  /deep/.u-checkbox__icon-wrap {
    background-color: #111111;
  }
  .select {
    width: 30rpx;
    height: 30rpx;
    margin: 2rpx 8rpx 0 0;
    image {
      width: 100%;
      height: 100%;
    }
  }
}

// text:first-child
</style>